package com.ruoyi.recruit.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.recruit.domain.RecruitContent;
import com.ruoyi.recruit.domain.RecruitInfo;
import com.ruoyi.recruit.domain.RecruitUser;
import com.ruoyi.recruit.service.IRecruitContentService;
import com.ruoyi.recruit.service.IRecruitInfoService;
import com.ruoyi.recruit.service.IRecruitUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.recruit.domain.RecruitUserConfirm;
import com.ruoyi.recruit.service.IRecruitUserConfirmService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 到岗人员Controller
 * 
 * @author ruoyi
 * @date 2022-05-05
 */
@Controller
@RequestMapping("/recruit/confirm")
public class RecruitUserConfirmController extends BaseController
{
    private String prefix = "recruit/confirm";

    @Autowired
    private IRecruitUserConfirmService recruitUserConfirmService;

    @Autowired
    private IRecruitInfoService recruitInfoService;

    @Autowired
    private IRecruitContentService recruitContentService;

    @Autowired
    private IRecruitUserService recruitUserService;

    @RequiresPermissions("recruit:confirm:view")
    @GetMapping(value = {"", "/{recruitId}"})
    public String confirm(@PathVariable(value = "recruitId", required = false) Long recruitId, ModelMap mmap)
    {
        RecruitInfo recruitInfo = new RecruitInfo();
        if(StringUtils.isNotNull(recruitId)){
            recruitInfo = recruitInfoService.selectRecruitInfoByRecruitId(recruitId);
        }
        mmap.put("recruitInfo", recruitInfo);
        mmap.put("infos", recruitInfoService.selectRecruitInfoList(new RecruitInfo()));
        return prefix + "/confirm";
    }

    /**
     * 查询到岗人员列表
     */
    @RequiresPermissions("recruit:confirm:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecruitUserConfirm recruitUserConfirm)
    {
        startPage();
        List<RecruitUserConfirm> list = recruitUserConfirmService.selectRecruitUserConfirmList(recruitUserConfirm);
        return getDataTable(list);
    }

    /**
     * 导出到岗人员列表
     */
    @RequiresPermissions("recruit:confirm:export")
    @Log(title = "到岗人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecruitUserConfirm recruitUserConfirm)
    {
        List<RecruitUserConfirm> list = recruitUserConfirmService.selectRecruitUserConfirmList(recruitUserConfirm);
        ExcelUtil<RecruitUserConfirm> util = new ExcelUtil<RecruitUserConfirm>(RecruitUserConfirm.class);
        return util.exportExcel(list, "到岗人员数据");
    }

    /**
     * 导入到岗人员
     * @param file
     * @param recruitId
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "到岗人员", businessType = BusinessType.IMPORT)
    @RequiresPermissions("recruit:confirm:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, Long recruitId, boolean updateSupport) throws Exception
    {
        ExcelUtil<RecruitUserConfirm> util = new ExcelUtil<RecruitUserConfirm>(RecruitUserConfirm.class);
        List<RecruitUserConfirm> confirmList = util.importExcel(file.getInputStream());
        String message = recruitUserConfirmService.importUserConfirm(confirmList, recruitId, updateSupport, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("recruit:confirm:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<RecruitUserConfirm> util = new ExcelUtil<RecruitUserConfirm>(RecruitUserConfirm.class);
        return util.importTemplateExcel("到岗人员");
    }

    /**
     * 新增到岗人员
     */
    @GetMapping("/add/{recruitId}")
    public String add(@PathVariable("recruitId") Long recruitId, ModelMap mmap)
    {
        RecruitInfo recruitInfo = new RecruitInfo();
        if(recruitId != 0){
            recruitInfo = recruitInfoService.selectRecruitInfoByRecruitId(recruitId);
        }
        mmap.put("recruitInfo", recruitInfo);
        mmap.put("infos", recruitInfoService.selectRecruitInfoList(new RecruitInfo()));
        return prefix + "/add";
    }

    /**
     * 新增保存到岗人员
     */
    @RequiresPermissions("recruit:confirm:add")
    @Log(title = "到岗人员", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecruitUserConfirm recruitUserConfirm)
    {
        recruitUserConfirm.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(recruitUserConfirmService.insertRecruitUserConfirm(recruitUserConfirm));
    }

    /**
     * 修改到岗人员
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        RecruitUserConfirm recruitUserConfirm = recruitUserConfirmService.selectRecruitUserConfirmById(id);
        mmap.put("recruitUserConfirm", recruitUserConfirm);
        mmap.put("infos", recruitInfoService.selectRecruitInfoList(new RecruitInfo()));
        return prefix + "/edit";
    }

    /**
     * 修改保存到岗人员
     */
    @RequiresPermissions("recruit:confirm:edit")
    @Log(title = "到岗人员", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecruitUserConfirm recruitUserConfirm)
    {
        recruitUserConfirm.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(recruitUserConfirmService.updateRecruitUserConfirm(recruitUserConfirm));
    }

    /**
     * 删除到岗人员
     */
    @RequiresPermissions("recruit:confirm:remove")
    @Log(title = "到岗人员", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recruitUserConfirmService.deleteRecruitUserConfirmByIds(ids));
    }
}
